﻿@model DataAccess.DB.XuatKho

@{
    ViewBag.Title = "Xuất kho";
}
<style>
    #NgayNhapKho {
        width: 120px;
    }

    #TongThanhToan {
        border: none;        
        font-weight: bold;        
    }
</style>
<script src="~/Scripts/CustomValidate.js" type="text/javascript"></script>
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    @Html.HiddenFor(model => model.ID); 
    
    <table>
        <tr>
            <td style="width: 50%; vertical-align: top">
                <fieldset>
                    <legend>Khách hàng</legend>
                    <table>
                        <tr>
                            <td class="editor-label" style="width: 40%">
                                <span>Mã khách hàng:</span>
                            </td>
                            <td class="editor-field">
                                @Html.HiddenFor(model => model.ID_KhachHang) <span style="font-size: 18px; font-weight: bold">@ViewBag.makhachhang</span>

                            </td>
                        </tr>
                        <tr>
                            <td class="editor-label" style="width: 20%">
                                <span>Tên khách hàng:</span>
                            </td>
                            <td class="editor-field">
                                <span style="font-size: 18px; font-weight: bold">@ViewBag.tenkhachhang</span>
                            </td>
                        </tr>
                        <tr>
                            <td class="editor-label" style="width: 20%">
                                <span>Địa chỉ khách hàng:</span>
                            </td>
                            <td class="editor-field">
                                <span style="font-size: 18px; font-weight: bold">@ViewBag.diachikhachhang</span>

                            </td>
                        </tr>
                    </table>
                </fieldset>
            </td>
            <td style="vertical-align: top">
                <fieldset>
                    <legend>Thông tin hóa đơn</legend>
                    <table>
                        <tr>
                            <td class="editor-label" style="width: 150px">
                                <span>Từ kho:</span><span style="color: red">*</span>
                            </td>
                            <td class="editor-field">
                                @Html.HiddenFor(model => model.KhoID, new { @readonly = true })
                                @ViewBag.TenKho
                            </td>
                        </tr>
                        <tr>
                            <td class="editor-label" style="width: 40%">
                                <span>Mã hóa đơn:</span><span style="color: red">*</span>
                            </td>
                            <td class="editor-field">
                                @Html.TextBoxFor(model => model.MaHoaDon)
                                <br />
                                @Html.ValidationMessageFor(model => model.MaHoaDon)
                            </td>
                        </tr>
                        <tr>
                            <td class="editor-label">
                                <span>Nhân viên giao dịch:</span><span style="color: red">*</span>
                                @Html.HiddenFor(model => model.ID_NguoiXuatKho)
                            </td>
                            <td class="editor-field">
                                @ViewBag.CurrentUserDisplayName
                            </td>
                        </tr>
                        <tr>
                            <td class="editor-label" style="width: 20%">
                                <span>Ngày xuất kho:</span><span style="color: red">*</span>
                            </td>
                            <td class="editor-field">
                                @Html.EditorFor(model => model.NgayXuatKho)
                                <br />
                                @Html.ValidationMessageFor(model => model.NgayXuatKho)
                            </td>
                        </tr>
                    </table>
                </fieldset>
            </td>
        </tr>
    </table>
    <table id="users" class="ms-form-table-hd" style="width:1000px">
        <thead>
            <tr>
                <th style="width:250px">Mã sản phẩm</th>
                <th style="width:150px">Số lượng</th>
                <th style="width:200px">Giá</th>
                <th style="width:200px">Thành tiền</th>
                <th style="width:150px">Đơn vị</th>
                <th style="width:50px"></th>
            </tr>
        </thead>
        <tbody>
            @{string[] listmasanpham = @ViewBag.listmasp;
              string[] listSoluong = @ViewBag.listsoLuong;
              string[] listgia = @ViewBag.listgia;
              string[] listDonvi = @ViewBag.listDonvi;
            }
            @if (listmasanpham != null)
            {
                for (int i = 1; i < listmasanpham.Length; i++)
                {
                    string id_SP = "ma_sp" + Convert.ToString(i);
                    string id_SL = "SoLuong_" + Convert.ToString(i);
                    string id_Gia = "Gia_" + Convert.ToString(i);
                    string id_DonVi = "DonVi" + Convert.ToString(i);
                    string id_ThanhTien = "ThanhTien_" + Convert.ToString(i);
                    string ThanhTien = DuocThaiBinh.Common.Util.ConvertDecimaToCurrency(Convert.ToInt32(listSoluong[i - 1]) * DuocThaiBinh.Common.Util.ConvertObjectToDecima(listgia[i - 1]));
                <tr>
                    <td>
                        <input  type="text" readonly="true" value="@listmasanpham[i - 1]" id="@id_SP" name="@id_SP" style="width:150px"/> <input id="@i" type="button" value="+" onclick="showattach(this.id)" style="font-weight:bold;width:30px"/>
                    </td>
                    <td>
                        <input type="text" value="@listSoluong[i - 1]" id="@id_SL" name="@id_SL" style="width:100px;text-align:right"/>
                    </td>
                    <td>
                        <input type="text" value="@listgia[i - 1]" id="@id_Gia" name="@id_Gia" style="width:180px;text-align:right"/>
                    </td>
                    <td>
                        <input type="text" value="@ThanhTien" id="@id_ThanhTien" name="@id_ThanhTien" style="width:180px;text-align:right"/>
                    </td>
                    <td>
                        <input type="text" value="@listDonvi[i - 1]" id="@id_DonVi" name="@id_DonVi"  style="width:100px"/>
                    </td>
                    <td>
                        <input type="button" value="xóa" class="delrow" style="width: 40px" />
                    </td>
                </tr>
                }
            }
        </tbody>
    </table>
    <table>
        <tr>
            <td style="width: 50%; text-align: left">
                <input type="button" id="btnAddSP" class="btnInsertRows" />
            </td>
            <td>Tổng tiền phải thanh toán: @Html.TextBoxFor(model => model.TongThanhToan, new { @readonly = "true" })
            </td>
        </tr>
    </table>

    <p>
        <input type="submit" value="Lưu hóa đơn" name="submit" />
        <input type="submit" name="submit" value="Lưu & in hóa đơn" />
        @Html.ActionLink("Xóa", "Delete", new { id = Model.ID }, new { onclick = "return confirm('Bạn có muốn xóa hóa đơn " + @Model.MaHoaDon + "')", @class = "linkCreate" }) @Html.ActionLink("Thoát", "IndexGrid", null, new { @class = "linkCreate" })
    </p>

    <div id="dialog-modal" style="display: none" title="Danh sách sản phẩm">
        
    </div>
    <div id="dialog-confirm" style="display: none" title="Lưu hóa đơn">
        <span>Bạn có muốn lưu hóa đơn ?</span>
    </div> 
}

<input type="hidden" id="rowsp" />
<input type="hidden" id="stt" value="@ViewBag.stt" name="stt" />
@Html.HiddenFor(p => p.TongThanhToan)


@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")


    <script type="text/javascript">
        $(document).ready(function () {

            $("input[id*='SoLuong_']").blur(function () {
                var TongThanhToan = 0;
                $("input[id*='SoLuong_']").each(function () {
                    var soluong = $(this).val();
                    var srow = this.id.split('_');
                    var gia = $("#Gia_" + srow[1]).val().replace(/,/g, '');
                    $('#ThanhTien_' + srow[1]).val(soluong * gia);
                    $('#ThanhTien_' + srow[1]).formatCurrency();
                    TongThanhToan += soluong * gia;
                });
                $('#TongThanhToan').val(TongThanhToan);
                $('#TongThanhToan').formatCurrency();

            });
            $("input[id*='Gia_']").blur(function () {
                $(this).formatCurrency();
                var TongThanhToan = 0;
                $("input[id*='Gia_']").each(function () {
                    var gia = $(this).val().replace(/,/g, '');
                    var srow = this.id.split('_');
                    var soluong = $("#SoLuong_" + srow[1]).val();
                    $('#ThanhTien_' + srow[1]).val(soluong * gia);
                    $('#ThanhTien_' + srow[1]).formatCurrency();
                    TongThanhToan += soluong * gia;

                });
                $('#TongThanhToan').val(TongThanhToan);
                $('#TongThanhToan').formatCurrency();
            });
            $(".delrow").click(function () {
                $(this).parent().parent().remove();
                var TongThanhToan = 0;
                $("input[id*='SoLuong_']").each(function () {
                    var soluong = $(this).val();
                    var srow = this.id.split('_');

                    var gia = $("#Gia_" + srow[1]).val().replace(/,/g, '');
                    $('#ThanhTien_' + srow[1]).val(soluong * gia);
                    $('#ThanhTien_' + srow[1]).formatCurrency();
                    TongThanhToan += soluong * gia;

                });
                $('#TongThanhToan').val(TongThanhToan);
                $('#TongThanhToan').formatCurrency();

            });

        });

        //confirm submit
        var showDialog = true;
        $("input[name='submit']").click(function (e) {
            var input = $(this);
            var form = $(this).parents("form");
            if (showDialog) {
                // Prevent submit
                e.preventDefault();
                // Show dialog
                $("#dialog-confirm").dialog({
                    resizable: false,
                    height: 140,
                    modal: true,
                    buttons: {
                        'Lưu': function () {
                            $(this).dialog('close');
                            // Get the parent form
                            showDialog = false;
                            //form.submit();
                            //input.submit();
                            $(input).trigger("click");
                        },
                        'Không': function () {
                            $(this).dialog('close');
                            showDialog = true;
                        }
                    }
                });
            }
        });

        function showattach(val) {
            $("#rowsp").val(val);
            $("#dialog-modal").dialog({
                width: 1000,
                height: 600,
                modal: true,
                open: function () {
                    $(this).load("/SanPham/IndexSPXK");
                },
            });
        }
        $("#btnAddSP").click(function () {
            var stt = $("#stt").val();
            $("#stt").val(parseInt(stt) + 1);
            $("#users tbody").append("<tr>" +
              "<td><input type='text' id='ma_sp" + stt + "' name='ma_sp" + stt + "' style='width:150px' readonly='true'/> <input type='button' id='" + stt + "' name='ma_sp" + stt + "' value='+' onclick='showattach(this.id);' style='font-weight:bold;width:30px'/></td>" +
              "<td><input type='text' id='SoLuong_" + stt + "' style='width:100px;text-align:right' name='SoLuong_" + stt + "' value='0'/></td>" +
              "<td><input type='text' id='Gia_" + stt + "' style='width:180px;;text-align:right' name='Gia_" + stt + "'/></td>" +
               "<td><input type='text' id='ThanhTien_" + stt + "' style='width:180px;;text-align:right' name='ThanhTien_" + stt + "' value='0'/></td>" +
              "<td><input type='text' id='DonVi" + stt + "'style='width:100px' name='DonVi" + stt + "'/></td>" +
              "<td><input type='button' value='xóa' id='delete" + stt + "'style='width:40px' class='delrow'/></td>" +
            "</tr>");
            //add ma san pham


            $("input[id*='SoLuong_']").blur(function () {
                var TongThanhToan = 0;
                $("input[id*='SoLuong_']").each(function () {
                    var soluong = $(this).val();
                    var srow = this.id.split('_');
                    var gia = $("#Gia_" + srow[1]).val().replace(',', '');
                    $('#ThanhTien_' + srow[1]).val(soluong * gia);
                    $('#ThanhTien_' + srow[1]).formatCurrency();
                    TongThanhToan += soluong * gia;
                });

                $('#TongThanhToan').val(TongThanhToan);
                $('#TongThanhToan').formatCurrency();

            });

            $("input[id*='Gia_']").blur(function () {
                $(this).formatCurrency();
                var TongThanhToan = 0;
                $("input[id*='Gia_']").each(function () {
                    var gia = $(this).val().replace(',', '');
                    var srow = this.id.split('_');
                    var soluong = $("#SoLuong_" + srow[1]).val();
                    $('#ThanhTien_' + srow[1]).val(soluong * gia);
                    $('#ThanhTien_' + srow[1]).formatCurrency();
                    TongThanhToan += soluong * gia;

                });

                $('#TongThanhToan').val(TongThanhToan);
                $('#TongThanhToan').formatCurrency();
            });
            $(".delrow").click(function () {
                $(this).parent().parent().remove();
                var TongThanhToan = 0;
                $("input[id*='SoLuong_']").each(function () {
                    var soluong = $(this).val();
                    var srow = this.id.split('_');

                    var gia = $("#Gia_" + srow[1]).val().replace(',', '');
                    TongThanhToan += soluong * gia;

                });

                $('#TongThanhToan').val(TongThanhToan);
                $('#TongThanhToan').formatCurrency();

            })
        });
              

        function SelectSP(sp) {
            var row = $("#rowsp").val();
            var selectedValue = sp;
            if (selectedValue != "Select") {
                $.ajax({
                    url: '@Url.Action("GetProductXKById", "SanPham")',
                    type: 'POST',
                    data: { "selectedValue": selectedValue },
                    dataType: 'json',
                    success: function (response) {
                        var productDetails = response.ProductDetail;

                        var productdetail = productDetails.split(';#');

                        $("#ma_sp" + row).val(productdetail[0]);
                        $("#Gia_" + row).val(productdetail[1]);
                        $("#Gia_" + row).formatCurrency();
                        $("#DonVi" + row).val(productdetail[2]);

                    }
                });
                $("#dialog-modal").dialog("close");
            }
        }
        $(':input').mouseover(function () {
            $(this).attr("title", $(this).val());
        });
    </script>
}
